/**
 * The AudioTrack Demo by Chris J. Rock. Aug 10, 2011
 * Check out chrisjrock.net and be a king among men.
 *
 * Copyright (c) 2011 Chris J. Rock
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 **/


package
{
	import flash.display.Sprite;
	
	import net.chrisjrock.sound.audioTrack.events.AudioTrackEvent;
	import net.chrisjrock.sound.audioTrack.mixer.AudioMixer;
	import net.chrisjrock.sound.audioTrack.mixer.QueueTrack;
	import net.chrisjrock.sound.audioTrack.mixer.SequenceTrack;
	import net.chrisjrock.sound.audioTrack.track.AudioTrack;
	import net.chrisjrock.ui.AudioTrackUi;
	
	[SWF(width='450', height='600', backgroundColor='#0', frameRate='60')]
	public class Main extends Sprite
	{
		//Switched to dynamically loaded assets to cut down on file-size.
		/*[Embed(source="asset/audio/music/The Rachels/Music for Egon Shiele/the rachels - music for egon schiele - 01 - family portrait.mp3")]
		private var _Source0:Class;
		
		[Embed(source="asset/audio/music/The Rachels/Music for Egon Shiele/the rachels - music for egon schiele - 02 - egon & gertie.mp3")]
		private var _Source1:Class;
		
		[Embed(source="asset/audio/music/The Rachels/Music for Egon Shiele/the rachels - music for egon schiele - 03 - first self-portrait series.mp3")]
		private var _Source2:Class;
		
		[Embed(source="asset/audio/music/The Rachels/Music for Egon Shiele/the rachels - music for egon schiele - 04 - mime van osen.mp3")]
		private var _Source3:Class;
		
		[Embed(source="asset/audio/music/The Rachels/Music for Egon Shiele/the rachels - music for egon schiele - 05 - second self-portrait series.mp3")]
		private var _Source4:Class;*/
		
		private static const URL0:String = "asset/audio/music/The Rachels/Music for Egon Shiele/the rachels - music for egon schiele - 01 - family portrait.mp3";
		private static const URL1:String = "asset/audio/music/The Rachels/Music for Egon Shiele/the rachels - music for egon schiele - 02 - egon & gertie.mp3";
		private static const URL2:String = "asset/audio/music/The Rachels/Music for Egon Shiele/the rachels - music for egon schiele - 03 - first self-portrait series.mp3";
		private static const URL3:String = "asset/audio/music/The Rachels/Music for Egon Shiele/the rachels - music for egon schiele - 04 - mime van osen.mp3";
		private static const URL4:String = "asset/audio/music/The Rachels/Music for Egon Shiele/the rachels - music for egon schiele - 05 - second self-portrait series.mp3";
		
		private var _masterMixer:AudioMixer;
		private var _track0:AudioTrack;
		private var _track1:QueueTrack;
		private var _track2:SequenceTrack;
		
		private var _uiContainer:Sprite;
		private var _masterUi:AudioTrackUi;
		private var _track0Ui:AudioTrackUi;
		private var _track1Ui:AudioTrackUi;
		private var _track2Ui:AudioTrackUi;
		
		public function Main()
		{
			init();
		}
		
		private function destroy ():void
		{
			if (_masterMixer)
			{
				_masterMixer.disposeAll();
				_masterMixer = null;
			}
			
			if (_uiContainer)
			{
				removeChild(_uiContainer);
				_uiContainer = null;
				
				_track0Ui.destroy();
				_track1Ui.destroy();
				_track2Ui.destroy();
				
				_track0Ui = _track1Ui = _track2Ui = null;
			}
		}
		
		private function init ():void
		{
			buildAudio();
			_track0.play();
			buildUi();
		}
		
		private function buildAudio ():void
		{
			var audioTrackIdPrefix:String = "AudioTrack:";
			
			//A single track.
			_track0 = new AudioTrack(audioTrackIdPrefix + URL0);
			_track0.load(URL0);
			
			
			
			//A subtrack.
			var track1_0:AudioTrack = new AudioTrack(audioTrackIdPrefix + URL1);
			track1_0.load(URL1);
			//Another subtrack
			var track1_1:AudioTrack = new AudioTrack(audioTrackIdPrefix + URL2);
			track1_1.load(URL2);
			
			//A mixer with 2 subtracks played as a queue.
			_track1 = new QueueTrack("Queue Track");
			_track1.addTrack(track1_0);
			_track1.addTrack(track1_1);
			
			
			
			//A subtrack.
			var track2_0:AudioTrack = new AudioTrack(audioTrackIdPrefix + URL3);
			track2_0.addEventListener(AudioTrackEvent.LOAD_COMPLETE , seqTrackLoadedHandler , false , 0 , true);
			track2_0.load(URL3);
			//Another subtrack
			var track2_1:AudioTrack = new AudioTrack(audioTrackIdPrefix + URL4);
			track2_1.addEventListener(AudioTrackEvent.LOAD_COMPLETE , seqTrackLoadedHandler , false , 0 , true);
			track2_1.load(URL4);
			
			//A mixer with 2 subtracks played as a sequence.
			_track2 = new SequenceTrack("SequenceTrack");
			_track2.addTrack(track2_0);
			_track2.addTrack(track2_1);
			
			
			
			//The master mixer is created and all tracks are added to it.
			_masterMixer = new AudioMixer("AudioMixer");
			_masterMixer.addTrack(_track0);
			_masterMixer.addTrack(_track1);
			_masterMixer.addTrack(_track2);
		}
		
		private function buildUi ():void
		{
			_masterUi = new AudioTrackUi("AudioMixer (Master)" , _masterMixer);
			
			_track0Ui = new AudioTrackUi("AudioTrack" , _track0);
			_track1Ui = new AudioTrackUi("QueueTrack" , _track1);
			_track2Ui = new AudioTrackUi("SequenceTrack" , _track2);
			
			_masterUi.init();
			_track0Ui.init();
			_track1Ui.init();
			_track2Ui.init();
			
			var buffer:Number = 20;
			_track0Ui.display.y = _masterUi.display.height + buffer;
			_track1Ui.display.y = _track0Ui.display.y + _track0Ui.display.height + buffer;
			_track2Ui.display.y = _track1Ui.display.y + _track1Ui.display.height + buffer;
			
			_uiContainer = new Sprite();
			_uiContainer.addChild(_masterUi.display);
			_uiContainer.addChild(_track0Ui.display);
			_uiContainer.addChild(_track1Ui.display);
			_uiContainer.addChild(_track2Ui.display);
			
			_uiContainer.x = 70;
			_uiContainer.y = 20;
			addChild(_uiContainer);
		}
		
		private function seqTrackLoadedHandler (event:AudioTrackEvent):void
		{
			event.target.removeEventListener(AudioTrackEvent.LOAD_COMPLETE , seqTrackLoadedHandler);
			_track2.update();
		}
	}
}